﻿using EasyShop.OrderService.App.Interface;
using EasyShop.OrderService.Repository.Domain;
using EasyShop.OrderService.Request;
using EasyShop.OrderService.Response;
using EasyShop.UserService.Enum;
using Grpc.Net.Client;
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EasyShop.OrderService.App
{
    public class OrderApp:IOrderApp
    {
        private readonly MySqlDBContext _mySqlDBContext;
        public OrderApp(MySqlDBContext mySqlDBContext)
        {
            _mySqlDBContext = mySqlDBContext;
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public OrderListView GetList(string userId)
        {
            var orderView = new OrderListView();
            var orders = _mySqlDBContext.Order.Where(c => c.UserId == userId).OrderByDescending(c=>c.CreateTime).ToList();
            if (orders.Count>0)
            {
                //gRPC:调用用户服务，查询用户详情
                var userServiceUrl = ServiceUrL.GetServiceUrlByName("UserService");
                using var channel_user = GrpcChannel.ForAddress(userServiceUrl);
                var client_user = new gUser.gUserClient(channel_user);
                var reply_user = client_user.Get(new UserRequest {  Id = userId });
                orderView.UserName = reply_user.UserName;

                //gRPC:调用商品服务，查询商品详情
                var orderServiceUrl = ServiceUrL.GetServiceUrlByName("GoodsService");
                var goodsIds = string.Join(",", orders.Select(c => c.GoodsId).ToList());
                using var channel_goods = GrpcChannel.ForAddress(orderServiceUrl);
                var client_goods = new gGoods.gGoodsClient(channel_goods);
                var reply_goods = client_goods.Get(new GoodsRequest { Id = goodsIds });
                var goodsViewList = new List<GoodsView>();
                foreach (var item in reply_goods.Goods)
                {
                    var order = orders.Where(c => c.GoodsId == item.Id).FirstOrDefault();
                    var goodsView = new GoodsView()
                    {
                        OrderId = order.Id,
                        Name = item.Name,
                        Price = item.Price.TryParseToDecimal(),
                        Description = item.Description,
                        Poster = item.Poster,
                        CreateTime = order.CreateTime
                    };
                    goodsViewList.Add(goodsView);
                }
                orderView.GoodsView = goodsViewList;
            }
            return orderView;
        }

        /// <summary>
        /// 购买商品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public string Buy(BuyReq request)
        {
            var order = new Order()
            {
                Id = Guid.NewGuid().ToString(),
                UserId = request.UserId,
                GoodsId = request.GoodsId,
                Status = (int)OrderStatusEnum.未付款,
                CreateTime = DateTime.Now,
            };
            _mySqlDBContext.Order.Add(order);
            _mySqlDBContext.SaveChanges();
            return "购买成功";
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public string Delete(string orderId)
        {
            var order = _mySqlDBContext.Order.Where(c => c.Id == orderId).FirstOrDefault();
            _mySqlDBContext.Order.Remove(order);
            _mySqlDBContext.SaveChanges();
            return "删除成功";
        }
    }
}
